package com.moxiao.yfjh.module.activity.dal.mysql.activityEnroll;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.mybatis.core.mapper.BaseMapperX;
import com.moxiao.yfjh.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollExportReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollPageReqVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.ActivityEnrollQueryVO;
import com.moxiao.yfjh.module.activity.controller.admin.activityEnroll.vo.EnrollUseVO;
import com.moxiao.yfjh.module.activity.controller.app.vo.AppEnrollUseVO;
import com.moxiao.yfjh.module.activity.dal.dataobject.activityEnroll.ActivityEnrollDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.poi.ss.formula.functions.T;

import java.math.BigDecimal;
import java.util.List;

/**
 * 相亲报名信息 Mapper
 *
 * @author mx
 */
@Mapper
public interface ActivityEnrollMapper extends BaseMapperX<ActivityEnrollDO> {

    default PageResult<ActivityEnrollDO> selectPage(ActivityEnrollPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ActivityEnrollDO>()
                .eqIfPresent(ActivityEnrollDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityEnrollDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityEnrollDO::getActivityId, reqVO.getActivityId())
                .likeIfPresent(ActivityEnrollDO::getUserName, reqVO.getUserName())
                .eqIfPresent(ActivityEnrollDO::getSex, reqVO.getSex())
                .betweenIfPresent(ActivityEnrollDO::getRegistTime, reqVO.getRegistTime())
                .eqIfPresent(ActivityEnrollDO::getPayAmount, reqVO.getPayAmount())
                .betweenIfPresent(ActivityEnrollDO::getPayTime, reqVO.getPayTime())
                .eqIfPresent(ActivityEnrollDO::getPayType, reqVO.getPayType())
                .eqIfPresent(ActivityEnrollDO::getPayNo, reqVO.getPayNo())
                .eqIfPresent(ActivityEnrollDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityEnrollDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityEnrollDO::getId));
    }

    default List<ActivityEnrollDO> selectList(ActivityEnrollExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ActivityEnrollDO>()
                .eqIfPresent(ActivityEnrollDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityEnrollDO::getActivityId, reqVO.getActivityId())
                .eqIfPresent(ActivityEnrollDO::getActivityStatus, reqVO.getActivityStatus())
                .eqIfPresent(ActivityEnrollDO::getUserId, reqVO.getUserId())
                .likeIfPresent(ActivityEnrollDO::getUserName, reqVO.getUserName())
                .eqIfPresent(ActivityEnrollDO::getSex, reqVO.getSex())
                .betweenIfPresent(ActivityEnrollDO::getRegistTime, reqVO.getRegistTime())
                .eqIfPresent(ActivityEnrollDO::getPayAmount, reqVO.getPayAmount())
                .betweenIfPresent(ActivityEnrollDO::getPayTime, reqVO.getPayTime())
                .eqIfPresent(ActivityEnrollDO::getPayType, reqVO.getPayType())
                .eqIfPresent(ActivityEnrollDO::getPayNo, reqVO.getPayNo())
                .eqIfPresent(ActivityEnrollDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ActivityEnrollDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ActivityEnrollDO::getId));
    }


    default List<ActivityEnrollDO> selectEnrollList(ActivityEnrollQueryVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ActivityEnrollDO>()
                .eqIfPresent(ActivityEnrollDO::getNo, reqVO.getNo())
                .eqIfPresent(ActivityEnrollDO::getActivityId, reqVO.getActivityId())
                .eqIfPresent(ActivityEnrollDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ActivityEnrollDO::getSex, reqVO.getSex())
                .eqIfPresent(ActivityEnrollDO::getStatus, reqVO.getStatus())
                .eqIfPresent(ActivityEnrollDO::getActivityStatus, reqVO.getActivityStatus())
                .orderByDesc(ActivityEnrollDO::getId));
    }


    /**
     * 根据活动id和性别及状态获得对应的报名数量
     *
     * @param activityId
     * @param sex
     */
    Integer getNumber(@Param("activityId") Long activityId, @Param("sex") Byte sex,
                      @Param("activityStatus") Byte activityStatus);


    /**
     * 关联分页查询
     * 分销员
     */
    @Select("select * from blind_activity_enroll")
    IPage<T> getPageList(Page<?> page, @Param("mobile") Long userId);


    /**
     * 报名人员list
     */
    @Select(" select t.id,t.activity_id,t.`status`,t.user_id,t.activity_status,t.is_pair,t.pair_id,t.selected,t1.* " +
            " from blind_activity_enroll t" +
            " join blind_member_user t1 on t.user_id = t1.id" +
            " where t.activity_id = #{activityId} and t1.sex = #{sex} and t.user_id != #{userId} " +
            " and t.status = 0 ")
    List<AppEnrollUseVO> getAllList(@Param("activityId") Long activityId, @Param("sex") Byte sex, @Param("userId") Long userId);

    /**
     * 报名人员list
     */
    @Select(" select t.id,t.activity_id,t.`status`,t.user_id,t.activity_status,t.is_pair,t.pair_id,t.selected,t1.* " +
            " from blind_activity_enroll t" +
            " join blind_member_user t1 on t.user_id = t1.id" +
            " where t.activity_id = #{activityId} and t.user_id != #{userId} " +
            " and t.status = 0 ")
    List<AppEnrollUseVO> getAllListNoSex(@Param("activityId") Long activityId, @Param("userId") Long userId);

    /**
     * 获得报名人数设置
     *
     * @return
     */
    @Select("select enroll_number from blind_basic_setting where id = 1;")
    Integer getEnrollNum();

    /**
     * 获取当前用户的性别
     */
    @Select(" select sex from blind_member_user " +
            " where  id = #{userId}")
    Byte getMemberSex(@Param("userId") Long userId);

    /**
     * 获得报名记录
     *
     * @param query
     * @return
     */
    List<EnrollUseVO> getActivityEnrollList(ActivityEnrollQueryVO query);

    /**
     * 获得当前活动的报名总金额
     */
    @Select("SELECT IFNULL(SUM(pay_amount) ,0.00)  from  blind_activity_enroll where activity_id = #{activityId} AND `status` = 0 AND deleted = 0")
    BigDecimal getActivityIdAmount(@Param("activityId") Long activityId);

    /**
     * 查询用户是否已报名
     * @param activityId
     * @param userId
     * @return
     */
    @Select("SELECT IFNULL(COUNT(*)  ,0) from blind_activity_enroll where activity_id = #{activityId} AND user_id =#{userId} AND status = 0 AND deleted = 0")
    Integer enrollNum(@Param("activityId") Long activityId,@Param("userId") Long userId);
}
